<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.lsl.endworkvol.entity.Activity" %>

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="renderer" content="webkit">
    <title>Activities Playground</title>
    <!-- 引入Bootstrap CSS -->
    <link rel="stylesheet" href="<%=request.getContextPath()%>/css/bootstrap.min.css">
    <!-- 引入Bootstrap JS和jQuery -->
    <script src="<%=request.getContextPath()%>/js/jquery.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
    <script src="<%=request.getContextPath()%>/js/bootstrap.min.js"></script>
    <style>
        .card {
            margin: 15px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="text-center mt-4">活动列表</h1>
        <div class="row">
            <%
                Date currentDate = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            %>
            <c:forEach var="activity" items="${activitiesList}">
                <%
                    String status = "未知";
                    Activity activity = (Activity) pageContext.getAttribute("activity");
                %>
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">${activity.title}</h5>
                            <h6 class="card-subtitle mb-2 text-muted">报名结束时间：<%=dateFormat.format(activity.getSignUpEndTime())%></h6>
                            <h6 class="card-subtitle mb-2 text-muted">活动开始时间：<%=dateFormat.format(activity.getStartTime())%></h6>
                            <p class="card-text">${activity.caption}</p>
                            <p class="card-text"><strong>状态:</strong> 
                                <%
                                    Date signUpEndTime = activity.getSignUpEndTime();
                                    Date activityStartTime = activity.getStartTime();
                                    if (currentDate.before(signUpEndTime)) {
                                        status = "报名中";
                                    }
                                    else if (currentDate.after(signUpEndTime) && currentDate.before(activityStartTime)) {
                                        status = "已结束报名";
                                    }
                                    else if (currentDate.after(activityStartTime)) {
                                        status = "进行中";
                                    }
                                    pageContext.setAttribute("status", status);
                                %>
                                ${status}(${activity.volunteerCount}/${activity.volunteerMaxCount})
                            </p>
                            <c:choose>
                                <c:when test="${status=='报名中' and activity.volunteerCount<activity.volunteerMaxCount}">
                                    <% session.setAttribute("userid", session.getAttribute("userid")); %>
                                    <a href="activity_signup/${activity.id}/${userid}" class="btn btn-primary">报名</a>
                                </c:when>
                            </c:choose>
                        </div>
                    </div>
                </div>
            </c:forEach>
        </div>
    </div>
</body>
</html>
